package com.wf.course.web.controller.comm;

import com.utils.page.PageBean;
import com.utils.push.JPush;
import com.wf.course.model.comm.PushLog;
import com.wf.course.service.IPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("push")
public class PushController {

    @Autowired
    private IPushService pushService;

    @RequestMapping("topush")
    public String toPush() {
        return "comm/push";
    }

    @RequestMapping("send")
    public String send(PushLog pushLog) {
        pushLog.setCreatetime(System.currentTimeMillis());
        Map<String, String> map = new HashMap<>();
        map.put("type", pushLog.getMsgtype() + "");
        map.put("param", pushLog.getParam());
        JPush.sendPush(pushLog.getTitle(), pushLog.getContent(), map, pushLog.getType());
        pushService.addLog(pushLog);
        return "redirect:/push/list";
    }

    @RequestMapping("list")
    public String list(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {
        Map<String, Object> map = null;
        try {
            map = new HashMap<>();
            PageBean<PushLog> pageBean = new PageBean<>(pageNo, pageSize, null, map);
            pushService.list(pageBean);
            pageBean.setUrl("push/list");
            model.addAttribute("page", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "comm/pushLogList";
    }
}
